package com.yuandengta.yomall.coupon.service.impl;

import com.yuandengta.yomall.coupon.entity.SeckillSkuRelationEntity;
import com.yuandengta.yomall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.coupon.dao.SeckillSessionDao;
import com.yuandengta.yomall.coupon.entity.SeckillSessionEntity;
import com.yuandengta.yomall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService skuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取最近三天的秒杀活动
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLast3DaySession() {
        List<SeckillSessionEntity> entities = list(new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", startTime(), endTime()));

        if (entities != null && entities.size() > 0) {
            // 根据活动场次查询出关联的商品
            List<SeckillSessionEntity> collect = entities.stream().map(session -> {
                Long sessionId = session.getId();
                List<SeckillSkuRelationEntity> skuRelationEntities = skuRelationService.list(
                        new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", sessionId));
                session.setRelationSkus(skuRelationEntities);
                return session;
            }).collect(Collectors.toList());

            return collect;
        }

        return null;
    }

    /**
     * 最近三天开始时间
     * @return
     */
    private String startTime(){
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        String format = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    /**
     * 最近三天结束时间
     * @return
     */
    private String endTime(){
        LocalDate plusDays = LocalDate.now().plusDays(2);
        LocalDateTime endTime = LocalDateTime.of(plusDays, LocalTime.MAX);
        String format = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}